﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#include "CellStableHeaders.h"
#include "CellStringConverter.h"

namespace cell
{

//-----------------------------------------------------------------------
StringConverter::StringConverter(void)
{
}
//-----------------------------------------------------------------------
String StringConverter::toString(float val, unsigned short precision, 
	unsigned short width, char fill, std::ios::fmtflags flags)
{
	stringstream stream;
	stream.precision(precision);
	stream.width(width);
	stream.fill(fill);
	if (flags)
		stream.setf(flags);
	stream << val;
	return stream.str();
}
//-----------------------------------------------------------------------
String StringConverter::toString(double val, unsigned short precision, 
	unsigned short width, char fill, std::ios::fmtflags flags)
{
	stringstream stream;
	stream.precision(precision);
	stream.width(width);
	stream.fill(fill);
	if (flags)
		stream.setf(flags);
	stream << val;
	return stream.str();
}
//-----------------------------------------------------------------------
String StringConverter::toString(int val, 
	unsigned short width, char fill, std::ios::fmtflags flags)
{
	stringstream stream;
	stream.width(width);
	stream.fill(fill);
	if (flags)
		stream.setf(flags);
	stream << val;
	return stream.str();
}

#if CELL_ARCH_TYPE == CELL_ARCHITECTURE_64 || CELL_PLATFORM == CELL_PLATFORM_APPLE || CELL_PLATFORM == CELL_PLATFORM_IOS
//-----------------------------------------------------------------------
String StringConverter::toString(unsigned int val, 
	unsigned short width, char fill, std::ios::fmtflags flags)
{
	stringstream stream;
	stream.width(width);
	stream.fill(fill);
	if (flags)
		stream.setf(flags);
	stream << val;
	return stream.str();
}
//-----------------------------------------------------------------------
/*String StringConverter::toString(size_t val, 
	unsigned short width, char fill, std::ios::fmtflags flags)
{
	stringstream stream;
	stream.width(width);
	stream.fill(fill);
	if (flags)
		stream.setf(flags);
	stream << val;
	return stream.str();
}*/
#if CELL_COMPILER == CELL_COMPILER_MSVC
//-----------------------------------------------------------------------
String StringConverter::toString(unsigned long val, 
	unsigned short width, char fill, std::ios::fmtflags flags)
{
	stringstream stream;
	stream.width(width);
	stream.fill(fill);
	if (flags)
		stream.setf(flags);
	stream << val;
	return stream.str();
}

#endif
//-----------------------------------------------------------------------
#else
String StringConverter::toString(size_t val, 
	unsigned short width, char fill, std::ios::fmtflags flags)
{
	stringstream stream;
	stream.width(width);
	stream.fill(fill);
	if (flags)
		stream.setf(flags);
	stream << val;
	return stream.str();
}
//-----------------------------------------------------------------------
String StringConverter::toString(unsigned long val, 
	unsigned short width, char fill, std::ios::fmtflags flags)
{
	stringstream stream;
	stream.width(width);
	stream.fill(fill);
	if (flags)
		stream.setf(flags);
	stream << val;
	return stream.str();
}
#endif

//-----------------------------------------------------------------------
String StringConverter::toString(long val, 
	unsigned short width, char fill, std::ios::fmtflags flags)
{
	stringstream stream;
	stream.width(width);
	stream.fill(fill);
	if (flags)
		stream.setf(flags);
	stream << val;
	return stream.str();
}
//-----------------------------------------------------------------------
String StringConverter::toString(uint64 val,
	unsigned short width, char fill, std::ios::fmtflags flags)
{
	stringstream stream;
	stream.width(width);
	stream.fill(fill);
	if (flags)
		stream.setf(flags);
	stream << val;
	return stream.str();
}
//-----------------------------------------------------------------------
String StringConverter::toString(bool val, bool yesNo)
{
	if (val)
	{
		if (yesNo)
		{
			return "yes";
		}
		else
		{
			return "true";
		}
	}
	else
	{
		if (yesNo)
		{
			return "no";
		}
		else
		{
			return "false";
		}
	}
}
//-----------------------------------------------------------------------
bool StringConverter::isNumber(const String& val)
{
	StringStream str(val);
	float tst;
	str >> tst;
	return !str.fail() && str.eof();
}
//-----------------------------------------------------------------------
int StringConverter::parseInt(const String& val)
{
	// Use istringstream for direct correspondence with toString
	StringStream str(val);
	int ret = 0;
	str >> ret;

	return ret;
}
//-----------------------------------------------------------------------
unsigned int StringConverter::parseUnsignedInt(const String& val)
{
	// Use istringstream for direct correspondence with toString
	StringStream str(val);
	unsigned int ret = 0;
	str >> ret;

	return ret;
}
//-----------------------------------------------------------------------
long StringConverter::parseLong(const String& val)
{
	// Use istringstream for direct correspondence with toString
	StringStream str(val);
	long ret = 0;
	str >> ret;

	return ret;
}
//-----------------------------------------------------------------------
unsigned long StringConverter::parseUnsignedLong(const String& val)
{
	// Use istringstream for direct correspondence with toString
	StringStream str(val);
	unsigned long ret = 0;
	str >> ret;

	return ret;
}
//-----------------------------------------------------------------------
uint64 StringConverter::parseUnsignedInteger64(const String& val)
{
	// Use istringstream for direct correspondence with toString
	StringStream str(val);
	uint64 ret = 0;
	str >> ret;

	return ret;
}
//-----------------------------------------------------------------------
bool StringConverter::parseBool(const String& val)
{
	return (StringUtil::startsWith(val, "1")
		|| StringUtil::startsWith(val, "true", true)
		|| StringUtil::startsWith(val, "yes", true));
}

} // end namespace cell
